import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# 题4：使用透视表计算星期与股票涨跌幅的关系
# 读取数据
df = pd.read_csv('data/stock.csv', usecols=['open', 'close', 'p_change'])
# 按照索引日期排序
df = df.sort_index()
# 1、先把对应的日期找到星期几
date = pd.to_datetime(df.index).weekday
df['week'] = date
# 2、假如把p_change按照大小去分个类0为界限
df['posi_neg'] = np.where(df['p_change'] > 0, 1, 0)
# 通过透视表，将整个过程变成更简单一些
count = df.pivot_table(['posi_neg'], index='week')
# 画图查看
count.plot(kind='bar', stacked=True)
plt.show()
